using System.Web.Mvc;
using ETestIT.Core;
using MvcContrib.Pagination;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Web.NHibernate;

namespace ETestIT.Web.Controllers
{
    [HandleError]
    public class JobTitlesController : Controller
    {
        private readonly IRepository<JobTitle> jobTitleRepository;

        public JobTitlesController(IRepository<JobTitle> jobTitleRepository)
        {
            Check.Require(jobTitleRepository != null, "jobTitleRepository may not be null");

            this.jobTitleRepository = jobTitleRepository;
        }

        [Transaction]
        public ActionResult Index(int? page)
        {
            var jobTitles = jobTitleRepository.GetAll().AsPagination(page ?? 1, 20);
            return View(jobTitles);
        }

        [Transaction]
        public ActionResult Show(int id)
        {
            JobTitle jobTitle = jobTitleRepository.Get(id);
            return View(jobTitle);
        }

        public ActionResult Create()
        {
            JobTitleFormViewModel viewModel = JobTitleFormViewModel.CreateJobTitleFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(JobTitle jobTitle)
        {
            if (ViewData.ModelState.IsValid && jobTitle.IsValid())
            {
                jobTitleRepository.SaveOrUpdate(jobTitle);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] =
                    "The jobTitle was successfully created.";
                return RedirectToAction("Index");
            }

            JobTitleFormViewModel viewModel = JobTitleFormViewModel.CreateJobTitleFormViewModel();
            viewModel.JobTitle = jobTitle;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id)
        {
            JobTitleFormViewModel viewModel = JobTitleFormViewModel.CreateJobTitleFormViewModel();
            viewModel.JobTitle = jobTitleRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(JobTitle jobTitle)
        {
            JobTitle jobTitleToUpdate = jobTitleRepository.Get(jobTitle.Id);
            TransferFormValuesTo(jobTitleToUpdate, jobTitle);

            if (ViewData.ModelState.IsValid && jobTitle.IsValid())
            {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] =
                    "The jobTitle was successfully updated.";
                return RedirectToAction("Index");
            }
            else
            {
                jobTitleRepository.DbContext.RollbackTransaction();

                JobTitleFormViewModel viewModel = JobTitleFormViewModel.CreateJobTitleFormViewModel();
                viewModel.JobTitle = jobTitle;
                return View(viewModel);
            }
        }

        private void TransferFormValuesTo(JobTitle jobTitleToUpdate, JobTitle jobTitleFromForm)
        {
            jobTitleToUpdate.Name = jobTitleFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id)
        {
            string resultMessage = "The jobTitle was successfully deleted.";
            JobTitle jobTitleToDelete = jobTitleRepository.Get(id);

            if (jobTitleToDelete != null)
            {
                jobTitleRepository.Delete(jobTitleToDelete);

                try
                {
                    jobTitleRepository.DbContext.CommitChanges();
                }
                catch
                {
                    resultMessage = "A problem was encountered preventing the jobTitle from being deleted. " +
                                    "Another item likely depends on this jobTitle.";
                    jobTitleRepository.DbContext.RollbackTransaction();
                }
            }
            else
            {
                resultMessage = "The jobTitle could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

        #region Nested type: JobTitleFormViewModel

        /// <summary>
        /// Holds data to be passed to the JobTitle form for creates and edits
        /// </summary>
        public class JobTitleFormViewModel
        {
            private JobTitleFormViewModel()
            {
            }

            public JobTitle JobTitle { get; internal set; }

            /// <summary>
            /// Creation method for creating the view model. Services may be passed to the creation 
            /// method to instantiate items such as lists for drop down boxes.
            /// </summary>
            public static JobTitleFormViewModel CreateJobTitleFormViewModel()
            {
                var viewModel = new JobTitleFormViewModel();

                return viewModel;
            }
        }

        #endregion
    }
}